package com.sptpc.demo1.service.impl;

import com.sptpc.demo1.model.Drink;
import com.sptpc.demo1.repository.DrinkRepository;
import com.sptpc.demo1.service.ICategoryService;
import com.sptpc.demo1.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 接口实现类
 */
@Service //标记该类为service处理类
public class DrinkjpaService implements IDrinkService {
    @Autowired  //在合适的地方自动实例化对象；
    private DrinkRepository drinkRepository;
    @Autowired
    private ICategoryService categoryService;
    @Override
    public Page<Drink> getAllDrinks(PageRequest pageRequest) {
        return drinkRepository.findAll(pageRequest);
        //findall()查询所有
    }

    @Override
    public void addNewDrink(Drink drink, Integer categoryId) {
        //1.通过categoryid查询出category
        //2.将category传递给drink
        drink.setCategory(categoryService.getCategoryById(categoryId));
        drinkRepository.save(drink);
        //save()进行新增
    }

    @Override
    public Drink getDrinkById(Integer id) {
        return drinkRepository.findById(id).orElse(null);//如果信息不为空则返回出信息，否则返回空
    }//通过id返回指定drink

    @Override
    public void updateDrink(Drink drink) {
        drinkRepository.save(drink);
    }//新增

    @Override
    public void delDrink(Integer id) {
        drinkRepository.deleteById(id);
    }//通过id进行删除
}
